package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.mapper.WmChannelMapper;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.wemwdia.dto.AdChannel;
import com.zhentao.model.wemwdia.dto.SensitiveDto;
import com.zhentao.model.wemwdia.pojo.WmChannel;
import com.zhentao.service.WmChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WmChannelServiceImpl implements WmChannelService {
    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public ResponseResult findAll() {
        List<WmChannel> wmChannels = wmChannelMapper.selectList(null);
        return ResponseResult.okResult(wmChannels);
    }

    @Override
    public ResponseResult list(SensitiveDto dto) {
        dto.checkParam();
        Page<WmChannel> page=new Page<>(dto.getPage(),dto.getSize());
        QueryWrapper<WmChannel> wrapper=new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.isNotBlank(dto.getName()), WmChannel::getName,dto.getName());
        page=wmChannelMapper.selectPage(page,wrapper);
        PageResponseResult result=new PageResponseResult();
        result.setData(page.getRecords());
        result.setCode(200);
        result.setSize((int) page.getSize());
        result.setTotal((int) page.getTotal());
        result.setCurrentPage((int) page.getCurrent());
        return result;
    }

    @Override
    public ResponseResult del(Integer id) {
        wmChannelMapper.deleteById(id);
        return ResponseResult.okResult(200, "删除成功");
    }

    @Override
    public ResponseResult save(AdChannel adChannel) {
        WmChannel wmChannel=new WmChannel();
        BeanUtils.copyProperties(adChannel, wmChannel);
        wmChannel.setCreatedTime(new Date());
        wmChannelMapper.insert(wmChannel);
        return ResponseResult.okResult(200, "新增成功");
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        WmChannel wmChannel=new WmChannel();
        BeanUtils.copyProperties(adChannel, wmChannel);
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult(200, "修改成功");
    }
}
